package com.dzqc.fly.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Clob;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONArray;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * 类名称: StringUtil
 * 类描述: 字符串转换工具类
 * 创建人: tonghl
 * 创建时间: 2022年06月29日 上午10:01:05
 * @version v1.0.0
 *
 */
public class StringUtil {
    public static String convertToString(Object obj) {
        String result = "";
        if(obj != null ){
            if(obj instanceof Clob){
                try {
                    Clob clob = (Clob)obj;
                    result = clob.getSubString(1, NumberUtil.convertToInt(clob.length()));
                } catch (Exception e) {
                    result = e.getMessage();
                }
            }else{
                result = obj.toString();
            }
        }
        return result;
    }
    public static boolean isChineseCharacter(String chineseStr){
        char[] charArray = chineseStr.toCharArray();
        for(int i = 0; i<charArray.length;i++){
            if((charArray[i]>=0x4e00)&&(charArray[i]<=0x9fbb)){
                return true;
            }
        }
        return false;
    }

    public static String BASE64DecoderString(String s){
        byte[] b = BASE64Decoder(s);
        return new String(b);
    }

    public static String BASE64Encoder(String s) throws UnsupportedEncodingException{
        byte[] b = s.getBytes("utf-8");
        return BASE64Encoder(b);
    }

    public static String BASE64Encoder(byte[] b){
        if(b != null && b.length>0){
            return new BASE64Encoder().encode(b);
        }
        return null;
    }

    public static byte[] BASE64Decoder(String s){
        try {
            if(s != null){
                return new BASE64Decoder().decodeBuffer(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String paramList2str(Collection list){
        return JSONArray.fromObject(list).toString().replaceAll("\"","'").replace("[", "").replace("]", "");
    }

    public static String list2str(Collection list){
        return list2str(list,",");
    }

    /**
     * 逗号隔开的字符串 去除空格
     * @param str
     * @return
     */
    public static String dhstrtrim(String str,String splitstr){
        StringBuilder sb = new StringBuilder();
        if(StringUtils.isNotEmpty(str)){
            String[] ss = str.split(splitstr);
            for (String s : ss) {
                sb.append(s.trim());
                sb.append(splitstr);
            }
            if(sb.length() > 0){
                sb.delete(sb.lastIndexOf(splitstr), sb.length());
            }
        }
        return sb.toString();
    }

    /**
     * 逗号隔开的字符串 去除空格
     * @param str
     * @return
     */
    public static String dhstrtrim(String str){
        return dhstrtrim(str, ",");
    }

    public static String list2str(Collection list,String splitstr){
        StringBuilder sb = new StringBuilder();
        if(null != list && list.size() > 0){
            for (Iterator iterator = list.iterator(); iterator.hasNext();) {
                String item = convertToString(iterator.next());
                sb.append(item);
                sb.append(splitstr);
            }
            if(sb.length() > 0){
                sb.delete(sb.lastIndexOf(splitstr), sb.length());
            }
        }
        return sb.toString();
    }

    public static List<String> str2list(String str){
        return str2list(str,",");
    }

    public static List<String> str2list(String str,String splitstr){
        String[] strs = str.split(splitstr);
        return Arrays.asList(strs);
    }

    public static String html2Text(String html){
        String ret = html;
        ret = ret.replaceAll("&[^;]*;", "");
        ret = ret.replaceAll("<[^>]*>", "");
        return ret;
    }
    public static String getLineSeparator(){
        return System.getProperty("line.separator");
    }

    public static String getRandomString(int length){
        StringBuilder ret=new StringBuilder();
        if(length>0){
            for(int i=0;i<length;i++){
                int num=(int)(Math.random()*10);
                ret.append(num);
            }
        }
        return ret.toString();

    }

    /**
     * 获取不为空并且去掉前后空格的字符串
     * @param target
     * @return
     */
    public static String getNotNullString(String target){
        if(target == null || target.length()==0){
            return "";
        } else {
            return target.trim();
        }
    }

    /**
     * 将{mobile}替换为params中key为mobile的值
     * @param serverName
     * @param params
     * @return
     */
    public static String dealString(String serverName, Map<String, Object> params) {
        String result = "";
        if(params == null) {
            result = serverName;
        }
        for(Entry<String, Object> entry :params.entrySet()){
            serverName = serverName.replaceAll("\\{"+entry.getKey()+"\\}", StringUtil.convertToString(entry.getValue()));
        }
        return serverName;
    }
}

